package com.example.demo.service;

import com.example.demo.model.Major;
import com.example.demo.model.College;
import com.example.demo.repository.MajorRepository;
import com.example.demo.repository.CollegeRepository;
import com.example.demo.repository.ClassRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.time.LocalDateTime;
import java.util.stream.Collectors;

@Service
public class MajorService {
    @Autowired
    private MajorRepository majorRepository;
    
    @Autowired
    private CollegeRepository collegeRepository;
    
    @Autowired
    private ClassRepository classRepository;

    public Page<Major> getAllMajors(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Major> majorPage = majorRepository.findAll(pageable);
        // 为每个专业填充关联信息
        return majorPage.map(this::populateMajorInfo);
    }
    
    // 添加搜索专业的方法
    public Page<Major> searchMajors(String name, Major.Status status, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        
        // 如果同时提供了名称和状态，则使用名称搜索
        if (name != null && !name.isEmpty()) {
            Page<Major> majorPage = majorRepository.findByNameContaining(name, pageable);
            // 为每个专业填充关联信息
            return majorPage.map(this::populateMajorInfo);
        }
        // 如果只提供了状态，则按状态搜索
        else if (status != null) {
            Page<Major> majorPage = majorRepository.findByStatus(status, pageable);
            // 为每个专业填充关联信息
            return majorPage.map(this::populateMajorInfo);
        }
        // 如果都没有提供，则返回所有专业
        else {
            Page<Major> majorPage = majorRepository.findAll(pageable);
            // 为每个专业填充关联信息
            return majorPage.map(this::populateMajorInfo);
        }
    }
    
    private Major populateMajorInfo(Major major) {
        // 填充学院信息
        if (major.getCollegeId() != null) {
            Optional<College> collegeOpt = collegeRepository.findById(major.getCollegeId());
            if (collegeOpt.isPresent()) {
                major.setCollegeName(collegeOpt.get().getName());
            }
        }
        
        // 填充班级数量信息
        if (major.getId() != null) {
            int classCount = classRepository.findByMajorId(major.getId()).size();
            major.setClassCount(classCount);
        }
        
        return major;
    }

    public Optional<Major> getMajorById(Long id) {
        Optional<Major> majorOpt = majorRepository.findById(id);
        if (majorOpt.isPresent()) {
            Major major = majorOpt.get();
            populateMajorInfo(major);
            return Optional.of(major);
        }
        return majorOpt;
    }

    public Major saveMajor(Major major) {
        return majorRepository.save(major);
    }

    public void deleteMajor(Long id) {
        majorRepository.deleteById(id);
    }

    public Optional<Major> getMajorByCodeAndCollegeId(String code, Long collegeId) {
        Optional<Major> majorOpt = majorRepository.findByCodeAndCollegeId(code, collegeId);
        if (majorOpt.isPresent()) {
            Major major = majorOpt.get();
            populateMajorInfo(major);
            return Optional.of(major);
        }
        return majorOpt;
    }

    public List<Major> getMajorsByName(String name) {
        List<Major> majors = majorRepository.findByNameContaining(name);
        // 为每个专业填充关联信息
        return majors.stream().map(this::populateMajorInfo).collect(Collectors.toList());
    }

    public List<Major> getMajorsByCollegeId(Long collegeId) {
        List<Major> majors = majorRepository.findByCollegeId(collegeId);
        // 为每个专业填充关联信息
        return majors.stream().map(this::populateMajorInfo).collect(Collectors.toList());
    }
    
    // 检查专业是否有关联的班级
    public boolean hasClasses(Long majorId) {
        return classRepository.findByMajorId(majorId).size() > 0;
    }
}